package com.fl.proyect_softmedic;


import com.example.proyect_softmedic.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class ViewchangeschedulleActivity extends Activity {
	
	//variables para el listado de agendas
    private TableLayout tabla;
    private TableLayout cabecera;
    private TableRow.LayoutParams layoutFila;
    private TableRow.LayoutParams layoutLong;
    private TableRow.LayoutParams layoutDefault;
    private Resources rs;
    private EditText getidschedulle;
    private TableRow fila;
    //variable para almacenar el roll del usuario
    private int roll_user;
    //CheckBox para agregar a cada registro 
    private CheckBox chk = null;
    private CheckBox chk2 = null;
    private CheckBox chk3 = null;
    //boton para regresar al menu de inicio
    private Button btnstart;
   
	@Override
		protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewchangeschedulle);
		

		roll_user = 1;//(int) (Math.random()*2+1);
		
		btnstart = (Button)findViewById(R.id.btnGoStart2);
		btnstart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(roll_user==1){
					Intent i = new Intent(ViewchangeschedulleActivity.this, Activity_medic.class);
					startActivity(i);
				}else{
					//Intent i = new Intent(ViewchangeschedulleActivity.this, Activity_medic.class);
					//startActivity(i);
				}
				
			}
		});
		
		
		
		//asignacion variables lista de agendas
        rs = this.getResources();
        tabla = (TableLayout)findViewById(R.id.tabla);
        cabecera = (TableLayout)findViewById(R.id.cabecera);
        layoutFila = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
        layoutLong = new TableRow.LayoutParams(200,TableRow.LayoutParams.WRAP_CONTENT);
        layoutDefault = new TableRow.LayoutParams(160,TableRow.LayoutParams.WRAP_CONTENT);
        
        //caja de texto para almacenar el id de la cita seleccionada por el usuario
        getidschedulle = (EditText)findViewById(R.id.editNum);
        getidschedulle.setFocusable(false);
        getidschedulle.setVisibility(View.INVISIBLE);
        getidschedulle.setKeyListener(null);
        

        addheader();
        addschedulle1(1);
        addschedulle2(2);
        addschedulle3(3);
        addbtnoptions(roll_user);
	}
	
		//muestra la cabecera de la tabla
		public void addheader(){
		//se definen los objetos a utilizar	
		TableRow fila;		
		TextView txtId_schedulle;
		TextView txtNombre;
		TextView txtsshedulle;
		
		//se asigna el valor del objeto fila y se le asigna el layout fila
		 fila = new TableRow(this);
		 fila.setLayoutParams(layoutFila);
		 
		 //se asignan valores a los objetos creados previamente
		 txtId_schedulle = new TextView(this);
		 txtNombre = new TextView(this);
		 txtsshedulle = new TextView(this);
	

		 //se asigna propiedades a el objeto txtid
		 txtId_schedulle.setText(rs.getString(R.string.id_schedulle));
		 txtId_schedulle.setGravity(Gravity.CENTER_HORIZONTAL);
		 txtId_schedulle.setTextAppearance(this,R.style.etiqueta);
		 txtId_schedulle.setBackgroundResource(R.drawable.styles_edittext);
		 txtId_schedulle.setLayoutParams(layoutDefault);

		//se asigna propiedades a el objeto txtnombre
		 txtNombre.setText(rs.getString(R.string.period_schedulle));
		 txtNombre.setGravity(Gravity.CENTER_HORIZONTAL);
		 txtNombre.setTextAppearance(this,R.style.etiqueta);
		 txtNombre.setBackgroundResource(R.drawable.styles_edittext);
		 //txtNombre.setLayoutParams(layoutLong);
		 
		//se asigna propiedades a el objeto txtsshedulle
		 txtsshedulle.setText(rs.getString(R.string.status_schedulle));
		 txtsshedulle.setGravity(Gravity.CENTER_HORIZONTAL);
		 txtsshedulle.setTextAppearance(this,R.style.etiqueta);
		 txtsshedulle.setBackgroundResource(R.drawable.styles_edittext);
		 txtsshedulle.setLayoutParams(layoutLong);

		 // se asignan cada uno de los objetos a la fila
		 fila.addView(txtId_schedulle);
		 fila.addView(txtNombre);
		 fila.addView(txtsshedulle);
		 cabecera.addView(fila);
		    }
		//muestra los botones dependiendo del roll del usuario
		public void addbtnoptions(int roll){

		     Button details = new Button(this);
		     Button modified = new Button(this);
		     
		     details.setText("Ver Detalles");
		     details.setBackgroundDrawable(getResources().getDrawable(R.drawable.styles_btn));
		     modified.setText("Modificar");
		     modified.setBackgroundDrawable(getResources().getDrawable(R.drawable.styles_btn));

	         fila = new TableRow(this);
	         fila.setLayoutParams(layoutFila);
	         
	         if(roll==1){
	         fila.addView(details);
	         fila.addView(modified);
	         }else{
	         fila.addView(details);	 
	         }
	         
	        
	       //si se muestra el boton detalles puede ver detalles de agenda
	         details.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent j = new Intent(ViewchangeschedulleActivity.this, DetailSchedulle.class);
					startActivity(j);
				}
			});
	         
	         //si se muestra el boton modified puede modificar agenda
	         modified.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent j = new Intent(ViewchangeschedulleActivity.this, OpenschedulleActivity.class);
					startActivity(j);
				}
			});
	          
	         tabla.addView(fila);
		}
		//muestra los registros 
		public void setregister(CheckBox c1, String id_schedulle, String date_schedulle, String status ){
			 
			 TextView txtId;
		     TextView txtNombre;
		     TextView txtsshedulle;
	         fila = new TableRow(this);
	         fila.setLayoutParams(layoutFila);
	         txtId = new TextView(this);	         
	         txtNombre = new TextView(this);
	         txtsshedulle = new TextView(this);
	         
	         txtId.setText(id_schedulle);
	         txtId.setGravity(Gravity.CENTER_HORIZONTAL);
	         txtId.setTextAppearance(this,R.style.etiqueta);
	         txtId.setBackgroundResource(R.drawable.tabla_celda);
	         txtId.setLayoutParams(layoutLong);
	         
	         txtNombre.setText(date_schedulle);
	         txtNombre.setGravity(Gravity.CENTER_HORIZONTAL);
	         txtNombre.setTextAppearance(this,R.style.etiqueta);
	         txtNombre.setBackgroundResource(R.drawable.tabla_celda);
	         txtNombre.setLayoutParams(layoutLong);
	         
	         txtsshedulle.setText(status);
	         txtsshedulle.setGravity(Gravity.CENTER_HORIZONTAL);
	         txtsshedulle.setTextAppearance(this,R.style.etiqueta);
	         txtsshedulle.setBackgroundResource(R.drawable.tabla_celda);
	         txtsshedulle.setLayoutParams(layoutDefault);
	         
	        
	         fila.addView(txtId);        
	         fila.addView(txtNombre);
	         fila.addView(txtsshedulle);
	         fila.addView(c1);
	         tabla.addView(fila);
		}
		//agrega la primera fila de registros
		public void addschedulle1(int id){
			chk = new CheckBox(this);
			setregister(chk, "20", "07-09-2013", "Disponible" );	         
		         final String n = Integer.toString(id);
		         
		         chk.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
		
							if(chk.isChecked()){	
								chk2.setChecked(false);
								chk3.setChecked(false);
							}else{
								
							}
								
							getidschedulle.setText(n);
						}
					});     
		    }
		//agrega la segunda fila de registros		
		public void addschedulle2(int id){
			chk2 = new CheckBox(this);
			setregister(chk2, "18", "25-09-2013", "Cerrada" );	         
		         final String n = Integer.toString(id);
		         
		         chk2.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
		
							if(chk2.isChecked()){
								
								chk.setChecked(false);
								chk3.setChecked(false);
							}else{
								
							}
								
							getidschedulle.setText(n);
						}
					});  
		    }//agrega la tercera fila de registros
		//agrega la tercera fila de registros
		public void addschedulle3(int id){
			chk3 = new CheckBox(this);
			setregister(chk3, "10", "2-09-2013", "Cerrada" );	         
		         final String n = Integer.toString(id);
		         
		         chk3.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
		
							if(chk3.isChecked()){
								
								chk.setChecked(false);
								chk2.setChecked(false);
							}else{
								
							}
								
							getidschedulle.setText(n);
						}
					});  
		    }

		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.viewchangeschedulle, menu);
		return true;
	}
}
